
//封装轮播

//轮播组件模型对象构造函数
function sliderModel () {
	/*
	 * 设置属性：1.轮播盒子；2.轮播每个步长对应的元素数组；3.索引元素；4.左按钮；5.右按钮
	 */
	this.sliderBox = arguments[0],
	this.sliderImg = arguments[1],
	this.sliderExtra = arguments[2],
	this.sliderLeftBtn = arguments[3],
	this.sliderRightBtn = arguments[4]
	
	//根据传入参数的个数，分别把参数值传给对应属性
	if(arguments.length==2){
		this.sliderExtra = null,
		this.sliderLeftBtn = null,
		this.sliderRightBtn = null
	}
	else if(arguments.length==3){
		this.sliderLeftBtn = null,
		this.sliderRightBtn = null
	}
	if(arguments.length==4){
		this.sliderExtra = null,
		this.sliderLeftBtn = arguments[2],
		this.sliderRightBtn = arguments[3]
	}
}

//轮播组件模型：1轮换属性；2.动画属性
function Sliders () {
	//设置属性：1.传入轮播组件；2.每步执行延迟；3每步执行时长；4.当前对应索引元素的颜色;5.其他索引元素的颜色
	this.sliderModel = arguments[0],
	this.sliderIf = arguments[1],
	this.spaceTime = arguments[2],
	this.transtionTime = arguments[3],
	this.currentColor = arguments[4],
	this.otherColor =arguments[5]
	
	//根据传入参数的个数以及传入参数的数据类型，分别把参数值传给对应属性
	if(arguments.length==4){
		if(typeof arguments[2]!=Number&&typeof arguments[3]!=Number){
			this.spaceTime = null,
			this.transtionTime = null,
			this.currentColor = arguments[2],
			this.otherColor =arguments[3]
		}
	}
	else if(arguments.length==3){
		if(typeof arguments[1]!=Number&&typeof arguments[2]!=Number){
			this.spaceTime = null,
			this.transtionTime = null,
			this.currentColor = arguments[1],
			this.otherColor =arguments[2]
		}
		else {
			this.spaceTime = arguments[1],
			this.transtionTime = arguments[2],
			this.currentColor = null,
			this.otherColor =null
		}
		this.sliderIf = null
	}
	else if(arguments.length==5){
		this.spaceTime = arguments[1],
		this.transtionTime = arguments[2],
		this.currentColor = arguments[3],
		this.otherColor =arguments[4],
		this.sliderIf = null
	}
}

//轮播组件执行函数
Sliders.prototype.go = function  () {
	var sliderBox = this.sliderModel.sliderBox;
	var sliderImg = this.sliderModel.sliderImg;
	var sliderExtra = this.sliderModel.sliderExtra;
	var sliderLeftBtn = this.sliderModel.sliderLeftBtn;
	var sliderRightBtn = this.sliderModel.sliderRightBtn;
	var width = sliderImg.eq(0)[0].clientWidth;
	var length = sliderImg.length;
	
	//设置左右按钮隐藏
	sliderLeftBtn.css('cursor','pointer').css('display','block');
	sliderRightBtn.css('cursor','pointer').css('display','block');
	
	//设置默认每步延迟和每步时长
	var spaceTime = (this.spaceTime!=null?this.spaceTime:3000);
	var transitionTime = (this.transitionTime!=null?this.transitionTime:500);
	
	//设置默认颜色属性
	var currentColor = (this.currentColor!=null?this.currentColor:"red");
	var otherColor = (this.otherColor!=null?this.otherColor:"darkslategray")
	if(sliderExtra!=null){
		sliderExtra.css('background',otherColor);
		sliderExtra.eq(0).css('background',currentColor)
	}
	
	
	//设置是否轮播默认值
	var sliderIf = (this.sliderIf!=null)?this.sliderIf:true;
	
	//设置轮播盒子的长度
	sliderBox[0].style.width = (length+1)*width+"px";
//	console.log(sliderBox[0].style.width+" :"+(length+1)*width)

	var count=-1;
	sliderImg.eq(0).clone().appendTo(sliderBox);

	//动画回调函数
	function run () {
		if(count==length+1){
			sliderBox.css('left',"0px")
			count=1;
			sliderBox.animate({'left':-(count)*width+"px"},transitionTime)
		}
		else{
			sliderBox.animate({'left':-(count)*width+"px"},transitionTime)
		}
		if(sliderExtra!==null){
			for (var i=0;i<length+1;i++) {
				if(count==i){
					sliderExtra.eq(i).css('background',currentColor)
				}
				else
					sliderExtra.eq(i).css('background',otherColor)
				if(count==length){
					sliderExtra.eq(0).css('background',currentColor)
					sliderExtra.eq(length-1).css('background',otherColor)
				}
			}
		}

	}
	//轮播事件
	if(sliderIf){
		var p = setInterval(function  () {
			count++;
			run();
		},spaceTime);
	}
	
	//当索引组件存在时添加监听事件
	if(sliderExtra!==null)
		sliderExtra.mouseenter(function(){
			if(sliderIf)
				clearInterval(p)
		}).click(function  () {
			count=(parseInt($(this).html())-1)
			run();
		}).mouseout(function  () {
			if(sliderIf)
				p=setInterval(run,spaceTime)
		});
	
	//轮播单体添加事件
	sliderImg.mousemove(function  () {
		sliderLeftBtn.css('visibility','visible');
		sliderRightBtn.css('visibility','visible');
		if(sliderIf)
		clearInterval(p)
	}).mouseout(function  () {
		// sliderLeftBtn.css('visibility','hidden');
		// sliderRightBtn.css('visibility','hidden');
		if(sliderIf){
			p=setInterval(function  () {
				count++;
				run()
			},spaceTime)
		}
	})
	
	//当左按钮组件存在时候添加监听事件
	if(sliderLeftBtn!==null)
		sliderLeftBtn.mouseenter(function  () {
			sliderLeftBtn.css('visibility','visible');
			sliderRightBtn.css('visibility','visible');
			if(sliderIf)
			clearInterval(p);
		}).click(function  () {
			if(sliderBox.is(":animated")){
				return false
			}
			if(count==-1)
				count=0;
			if(count==0){
				sliderBox.css('left',-length*width+"px")
				count=length;
			}
			count-- ; 
			run();
		}).mouseout(function  () {
			// sliderLeftBtn.css('visibility','hidden');
			// sliderRightBtn.css('visibility','hidden');
			if(sliderIf){
				p=setInterval(function  () {
					count++;
					run()
				},spaceTime)
			}
		});
	
	//当右按钮存在是添加监听事件
	if(sliderRightBtn!==null)
		sliderRightBtn.mouseenter(function  () {
			sliderLeftBtn.css('visibility','visible');
			sliderRightBtn.css('visibility','visible');
			if(sliderIf)
				clearInterval(p);
		}).click(function  () {
			if(count==-1)
				count=0;
			if(sliderBox.is(":animated")){
				return false
			}
			count++;
			run();
		}).mouseout(function  () {
			// sliderLeftBtn.css('visibility','hidden');
			// sliderRightBtn.css('visibility','hidden');
			if(sliderIf){
				p=setInterval(function  () {
					count++;
					run()
				},spaceTime)
			}
		});
}